package com.gamenew.bluetoothdemo.ui.fragment;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.MediaPlayer;
import android.media.audiofx.Visualizer;
import android.os.IBinder;

import com.gamenew.bluetoothdemo.adapter.CommonAdapter;
import com.gamenew.bluetoothdemo.bean.MusicBean;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;

public class MusicService extends Service {
    private int currentMusic = 0;
    private boolean isRecycle = true;//是否循环播放
    private boolean isInner = true;//是否播放内置的歌曲
    private List<MusicBean> innerList = new ArrayList<>();
    private List<MusicBean> outerList = new ArrayList<>();
    private List<MusicBean> musicList = new ArrayList<>();
    private CommonAdapter<MusicBean> musicAdapter;
    private MediaPlayer mediaPlayer;
    private AudioManager systemService;
    private Visualizer visualizer;
    private byte[] bytes;
    private Timer timer;
    private final static String PLAYING = "play";
    private final static String PAUSE = "pause";
    private final static String NEXT = "next";
    private final static String LAST = "last";
    private MusicBean musicBean;

    static final int SAMPLE_RATE_IN_HZ = 8000;
    static final int BUFFER_SIZE = AudioRecord.getMinBufferSize(SAMPLE_RATE_IN_HZ,
            AudioFormat.CHANNEL_IN_DEFAULT, AudioFormat.ENCODING_PCM_16BIT);
    AudioRecord mAudioRecord;

    public MusicService() {

    }

    @Override
    public void onCreate(){
        super.onCreate();
        init();
    }

    public void init(){
    }


    @Subscribe
    public void getMusicBeanId(MusicBean musicBean){
        this.musicBean = musicBean;
    }

    @Subscribe
    public void Player(MediaPlayer mediaPlayer){
        this.mediaPlayer = mediaPlayer;
        mediaPlayer.start();
    }


    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }


    class MsgReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent){
            switch(intent.getAction()){
                case PLAYING:
                    break;

                case PAUSE:
                    mediaPlayer.stop();
                    break;

                case LAST:

                    break;

                case NEXT:
                    break;
            }
        }
    }
}
